package com.example.mapp.buildSql;

import com.example.entity.detection.ExtDetectionElevator;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

public class DetectionElevatorMappSql {

    public String queryDetectionElevator(ExtDetectionElevator detectionElevator){
        StringBuilder builder = new StringBuilder("SELECT sy.party_name as usedPartyName,wb.party_name as maintenancePartyName," +
                "C.type_name as typeName,A.* FROM detection_elevator A " +
                "  left join elevator B on A.elevator_num = B.elevator_num\n" +
                "  left join elevator_type C on C.id = B.elevator_type_id\n" +
                "  left join party sy on sy.id = B.used_party_id\n" +
                "  left join party wb on wb.id = B.maintenance_party_id where 1=1");
        if(StringUtils.isNotEmpty(detectionElevator.getPlanDetailId())){
            builder.append(" and plan_detail_id = #{planDetailId}");
        }
        if(StringUtils.isNotEmpty(detectionElevator.getMaintenancePartyName())){
            builder.append(" and wb.party_name like").append(" '%").append(detectionElevator.getMaintenancePartyName()).append("%'");
        }
        if(StringUtils.isNotEmpty(detectionElevator.getUsedPartyName())){
            builder.append(" and sy.party_name like").append(" '%").append(detectionElevator.getUsedPartyName()).append("%'");
        }
        if(StringUtils.isNotEmpty(detectionElevator.getStartDate())){
            builder.append(" and check_time > #{checkTime}");
        }
        if(StringUtils.isNotEmpty(detectionElevator.getEndDate())){
            builder.append(" and check_time < #{checkTime}");
        }
        return  builder.toString();
    }

    public String updateDetectionElevator(ExtDetectionElevator detectionElevator){
        StringBuilder builder = new StringBuilder("update detection_elevator set ");
        StringBuilder setValue = new StringBuilder();
        if(StringUtils.isNotEmpty(detectionElevator.getPic1())){
            setValue.append(" set pic1 = {#pic1},");
        }
        if(StringUtils.isNotEmpty(detectionElevator.getPic1())){
            setValue.append(" set pic2 = {#pic2},");
        }
        if(StringUtils.isNotEmpty(detectionElevator.getPic1())){
            setValue.append(" set pic3 = {#pic3},");
        }
        if(StringUtils.isNotEmpty(detectionElevator.getCheckTime())){
            setValue.append(" set check_time = #{checkTime},");
        }
        if(StringUtils.isNotEmpty(detectionElevator.getReportUrl())){
            setValue.append(" set report_url = #{reportUrl},");
        }
        if(StringUtils.isNotEmpty(detectionElevator.getOriginalRecordUrl())){
            setValue.append(" set original_record_url = #{originalRecordUrl},");
        }
        if(StringUtils.isNotEmpty(detectionElevator.getBeCheckPartyUserSign())){
            setValue.append(" set be_check_party_user_sign = #{beCheckPartyUserSign},");
        }
        if(StringUtils.isNotEmpty(detectionElevator.getBeCheckPartyUserTime())){
            setValue.append(" set be_check_party_user_time = #{beCheckPartyUserTime},");
        }
        if(StringUtils.isNotEmpty(detectionElevator.getDetecteUserSignFirst())){
            setValue.append(" set detecte_user_sign_first = #{detecte_user_sign_first},");
        }
        if(StringUtils.isNotEmpty(detectionElevator.getDetecteUserSignTimeFirst())){
            setValue.append(" set detecte_user_sign_time_first = #{detecteUserSignTimeFirst},");
        }
        if(StringUtils.isNotEmpty(detectionElevator.getDetecteUserSignSecond())){
            setValue.append(" set detecte_user_sign_second = #{detecteUserSignSecond},");
        }
        if(StringUtils.isNotEmpty(detectionElevator.getDetecteUserSignTimeSecond())){
            setValue.append(" set detecte_user_sign_time_second = #{detecteUserSignTimeSecond},");
        }
        if(StringUtils.isNotEmpty(detectionElevator.getIsPass())){
            setValue.append(" set is_pass = #{isPass}");
        }
        builder.append(setValue.substring(0,setValue.lastIndexOf(","))).append(" where 1=1 ");

        if(detectionElevator.getId() != null){
            builder.append(" and id = #{id}");
        }
        return  builder.toString();
    }

    public String countElevator(Map map){
        StringBuilder builder = new StringBuilder("select count(1) as totalCount,\n" +
                "    sum(case when is_pass = '0' then 1 else 0 end) as notCheckCount from detection_elevator where 1=1 ");
        if(map.get("planDetailId") != null && !"".equals(map.get("planDetailId"))){
            builder.append(" and plan_detail_id = #{planDetailId}");
        }
        return builder.toString();
    }
}
